<?php
// 腾讯微博的api登录接口
class tqq{

	private $config;
	public function __construct()
	{
		//为用户表创建qq_id
		$api_config = require FANWE_ROOT."login/api_config.php";
		$this->config = $api_config['tqq'];

	}
	public function get_bind_info()
	{
		$data['name'] = "腾讯微博";
		$data['class'] = "tqq";
		$data['logo'] = APP_ROOT.'/login/tqq/bind_tqq.png';
		$uid = intval($GLOBALS['_FANWE']['uid']);
		if(FDB::resultFirst("select tqq_id from ".FDB::table("user")." where uid = ".$uid)!='')
		$data['is_bind'] = 1;
		else
		$data['is_bind'] = 0;
		$data['bind_url'] = APP_ROOT."/login.php?bind=tqq";
		$data['unbind_url'] = APP_ROOT."/login.php?unbind=tqq";
		$data['is_syn'] = $this->config['is_syn'];
		$syn_config = FDB::fetchFirst("select tqq_syn_weibo,tqq_syn_topic from ".FDB::table("user")." where uid = ".$uid);
		$data['syn_weibo'] = $syn_config['tqq_syn_weibo'];
		$data['syn_topic'] = $syn_config['tqq_syn_topic'];
		return $data;
	}

	public function get_url()
	{
        global $_FANWE;
		return $_FANWE['site_root']."login.php?mod=tqq";
	}

	public function get_api_url()
	{
		$url = APP_ROOT."/login.php?mod=tqq";
		$str = "<a href='".$url."' title='腾讯微博登录'><img src='".APP_ROOT."/login/tqq/login_tqq.png' alt='腾讯微博登录' /></a>&nbsp;";
		return $str;
	}

	public function jump()
	{
		require_once FANWE_ROOT.'login/tqq/opent.php';
		define( "MB_RETURN_FORMAT" , 'json' );
		define( "MB_API_HOST" , 'open.t.qq.com' );

		$o = new MBOpenTOAuth( $this->config['app_key'],$this->config['app_secret'] );
		$keys = $o->getRequestToken(get_domain().APP_ROOT."/login.php?rel=tqq");//这里填上你的回调URL
		$aurl = $o->getAuthorizeURL( $keys['oauth_token'] ,false,'');

        global $_FANWE;
		$login_oauth = array();
		$login_oauth['keys'] = $keys;
		fSetCookie('login_oauth', authcode(serialize($login_oauth), 'ENCODE',md5($_FANWE['config']['security']['authkey'])));
		fHeader("location:".$aurl);
	}

	public function bindjump()
	{
		require_once FANWE_ROOT.'login/tqq/opent.php';
		define( "MB_RETURN_FORMAT" , 'json' );
		define( "MB_API_HOST" , 'open.t.qq.com' );

		$o = new MBOpenTOAuth( $this->config['app_key'],$this->config['app_secret'] );
		$keys = $o->getRequestToken(get_domain().APP_ROOT."/login.php?bindrel=tqq");//这里填上你的回调URL
		$aurl = $o->getAuthorizeURL( $keys['oauth_token'] ,false,'');

        global $_FANWE;
		$login_oauth = array();
		$login_oauth['keys'] = $keys;
        fSetCookie('login_oauth', authcode(serialize($login_oauth), 'ENCODE',md5($_FANWE['config']['security']['authkey'])));
		fHeader("location:".$aurl);
	}


	public function bindcallback()
	{
		require_once FANWE_ROOT.'login/tqq/opent.php';
		define( "MB_RETURN_FORMAT" , 'json' );
		define( "MB_API_HOST" , 'open.t.qq.com' );

        global $_FANWE;
        $_FANWE['login_oauth'] = unserialize(authcode($_FANWE['cookie']['login_oauth'], 'DECODE',md5($_FANWE['config']['security']['authkey'])));

		$o = new MBOpenTOAuth( $this->config['app_key'],$this->config['app_secret'] , $_FANWE['login_oauth']['keys']['oauth_token'] , $_FANWE['login_oauth']['keys']['oauth_token_secret']  );
		$last_key = $o->getAccessToken(  $_REQUEST['oauth_verifier'] ) ;//获取ACCESSTOKEN
		$tencent_id = $last_key['name'];

        $url = 'http://open.t.qq.com/api/user/info?f=1';
		$params = array(
			'format' => MB_RETURN_FORMAT
		);

		$user_info = $o->get($url,$params);
		$user_info = $user_info['data'];

		$msg['id'] = $tencent_id;
		$uid = intval($_FANWE['uid']);
		
		$is_bind = FDB::resultFirst("select count(*) from ".FDB::table("user")." where uid <> '".$uid."' AND tqq_id = '".$msg['id']."'");
		if($is_bind > 0)
		{
			$user_info['tqq_id'] = $msg['id'];
			$user_info['bind_type'] = 'tqq';
			$user_info['tqq_app_key'] = $last_key['oauth_token'];
			$user_info['tqq_app_secret'] = $last_key['oauth_token_secret'];
			$user_info['short_name'] = $this->config['short_name'];
			fSetCookie('sync_bind_exists', authcode(serialize($user_info), 'ENCODE'),1209600);
		}
		else
		{
			FDB::query("update ".FDB::table("user")." set tqq_app_key = '".$last_key['oauth_token']."',tqq_app_secret = '".$last_key['oauth_token_secret']."',tqq_id='".$tencent_id."' where uid = ".$uid);
			
			if(!empty($user_info['head']) && !FS('User')->getIsAvatar($uid))
			{
				$img = copyFile($user_info['head'].'/180');
				if($img !== false)
					FS('User')->saveAvatar($uid,$img['path']);
			}
		}

		fHeader("location: ".FU('settings/bind'));

	}

	public function unbind()
	{
		$uid = intval($GLOBALS['_FANWE']['uid']);
		FDB::query("update ".FDB::table("user")." set tqq_id='',tqq_app_key='',tqq_app_secret='' where uid = ".$uid);
		fHeader("location: ".FU('settings/bind'));
	}


	public function callback()
	{
		require_once FANWE_ROOT.'login/tqq/opent.php';
		define( "MB_RETURN_FORMAT" , 'json' );
		define( "MB_API_HOST" , 'open.t.qq.com' );

        global $_FANWE;
        $_FANWE['login_oauth'] = unserialize(authcode($_FANWE['cookie']['login_oauth'], 'DECODE',md5($_FANWE['config']['security']['authkey'])));
		$o = new MBOpenTOAuth( $this->config['app_key'],$this->config['app_secret'] , $_FANWE['login_oauth']['keys']['oauth_token'] , $_FANWE['login_oauth']['keys']['oauth_token_secret']  );
		$last_key = $o->getAccessToken(  $_REQUEST['oauth_verifier'] ) ;//获取ACCESSTOKEN
		$tencent_id = $last_key['name'];

        $url = 'http://open.t.qq.com/api/user/info?f=1';
		$params = array(
			'format' => MB_RETURN_FORMAT
		);

		$user_info = $o->get($url,$params);
		$user_info = $user_info['data'];
		
		$msg['id'] = $tencent_id;
		$msg['name'] = $tencent_id;

		$sql = 'SELECT uid,password FROM '.FDB::table('user')." WHERE tqq_id = '".$msg['id']."'";
		$user = FDB::fetchFirst($sql);
		if(!empty($user) && $user['uid'] > 0)
		{
			$user = array(
                'uid'=>$user['uid'],
                'password'=>$user['password'],
            );
			
			if(!empty($user_info['head']) && !FS('User')->getIsAvatar($user['uid']))
			{
				$img = copyFile($user_info['head'].'/180');
				if($img !== false)
					FS('User')->saveAvatar($user['uid'],$img['path']);
			}
			
			FS('User')->setSession($user,1209600);
			FDB::query("update ".FDB::table("user")." set tqq_app_key = '".$last_key['oauth_token']."',tqq_app_secret = '".$last_key['oauth_token_secret']."',tqq_id='".$tencent_id."' where uid = ".$user['uid']);

			fHeader("location: ".FU('index'));
		}
		else
		{
			$password = random(6,1);
			$user_name = $msg['name'];
			do
			{
				$max_count = FDB::resultFirst("select count(*) from ".FDB::table("user")." where user_name = '".$user_name."'");
				if($max_count > 0)
					$user_name = $msg['name'].'_'.random(3);
			}
			while($max_count > 0);
			
			//开始自动注册会员
			$data = array(
				'user_name'        => $user_name,
                'password'        => md5($password),
				'tqq_id'           => $msg['id'],
				'tqq_app_key'	=>	$last_key['oauth_token'],
				'tqq_app_secret'	=>	$last_key['oauth_token_secret'],
				'user_name_match'=>segmentToUnicode($user_name),
				'status'    => 1,
                'gid' => 7,
				'reg_time' => TIME_UTC,
			);
			
			$uid = FDB::insert('user',$data,true);
			if($uid > 0)
			{
				unset($data);
				FDB::insert('user_count',array('uid' => $uid));
				
				$gender = $user_info['sex'] == 2 ? 0:1;
				
				$user_profile = array(
					'uid' => $uid,
					'gender' => $gender,
					'nick_name'	=> $user_info['nick'],
				);
				FDB::insert('user_profile',$user_profile);
				unset($user_profile);

				$user_status = array(
					'uid' => $uid,
					'reg_ip' => $GLOBALS['_FANWE']['client_ip'],
					'last_ip' => $GLOBALS['_FANWE']['client_ip'],
					'last_time' => TIME_UTC,
					'last_activity' => TIME_UTC,
				);
				FDB::insert('user_status',$user_status);

				$user = array(
                    'uid'=>$uid,
                    'password'=>md5($password),
                );
				
				if(!empty($user_info['head']))
				{
					$img = copyFile($user_info['head'].'/180');
					if($img !== false)
						FS('User')->saveAvatar($uid,$img['path']);
				}

                fSetCookie('sync_password', authcode($password, 'ENCODE'),1209600);

                FS('User')->setSession($user,1209600);
                fHeader("location: ".FU('settings/password'));
			}
		}
	}

	public function sendMessage($data)
	{
		static $client = NULL;
		if($client === NULL)
		{
			define( "MB_RETURN_FORMAT" , 'json' );
			define( "MB_API_HOST" , 'open.t.qq.com' );
			$res = require_once FANWE_ROOT.'login/tqq/api_client.php';
			$uid = intval($GLOBALS['_FANWE']['uid']);
			$udata = FDB::fetchFirst("select tqq_app_key,tqq_app_secret from ".FDB::table("user")." where uid = ".$uid);
	
			$client = new MBApiClient( $this->config['app_key'],$this->config['app_secret'],$udata['tqq_app_key'],$udata['tqq_app_secret']);
		}
		
		$data['content'] .= ' '.$data['url'];
		$p['c'] = $data['content'];
		if(!empty($data['img']))
			$p['p'] = FANWE_ROOT.$data['img'];
		
		$p['ip'] = $data['ip'];
		$p['type']	=1;
		
		try
		{
			$msg = $client->postOne($p);
			//print_r($msg);
		}
		catch(Exception $e)
		{
			//print_r($e);
		}
	}
}
?>